﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerCreator.LMS.BBS.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common;
using Telerik.WebControls;

public partial class Theme_3_Theme :PowerCreator.LMS.Web.Base.WebBasePage
{
    PowerCreator.LMS.Web.BBS.ManagerBBS opBBS = new PowerCreator.LMS.Web.BBS.ManagerBBS();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected string itype = "0";

    string AccessoryVedio = "<object align=\"right\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" id=\"MediaPlayer\"" +
               " width=\"400\" height=\"300\" type=\"application/x-oleobject\" standby=\"Loading Microsoft? Windows? Media Player components...\">"+
               "<param name=\"URL\" value=\"{0}\">"+
                "<param name=\"rate\" value=\"1\">"+
                "<param name=\"balance\" value=\"0\">"+
               " <param name=\"currentPosition\" value=\"0\">"+
                "<param name=\"defaultFrame\">"+
                "<param name=\"playCount\" value=\"1\">"+
                "<param name=\"autoStart\" value=\"-1\">"+
                "<param name=\"currentMarker\" value=\"0\">"+
                "<param name=\"invokeURLs\" value=\"-1\">"+
                "<param name=\"baseURL\">"+
               " <param name=\"volume\" value=\"100\">"+
                "<param name=\"mute\" value=\"0\">"+
                "<param name=\"uiMode\" value=\"full\">"+
                "<param name=\"stretchToFit\" value=\"-1\">"+
                "<param name=\"windowlessVideo\" value=\"0\">"+
                "<param name=\"enabled\" value=\"-1\">"+
                "<param name=\"enableContextMenu\" value=\"-1\">"+
                "<param name=\"fullScreen\" value=\"0\">"+
                "<param name=\"SAMIStyle\">"+
                "<param name=\"SAMILang\">"+
                "<param name=\"SAMIFilename\">"+
                "<param name=\"captioningID\">"+
            "</object>";
     string Accessory= "<a href=\"{0}\">{1}</a>";
    protected string url = string.Empty;
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();

        if (!IsPostBack)
        {
            ViewState["Type"] = "0";
            itype = Request.QueryString["Type"].ToString();
            ViewState["Type"] = itype;
            
            ViewState["DomainID"] = "0";
            ViewState["ThemeID"] = "0";
            string themeid = Request.QueryString["ThemeID"].ToString();
            ViewState["ThemeID"] = themeid;
            BandTitle();

            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
        }
    }
    private void CheckUser()
    {
        string url = "UserLogin.aspx";
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }
    protected override void InitPage()
    {
        //btn_Answer.Text = GetLanageValueByKey("btn_EditNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_Back.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Edit.Text = GetLanageValueByKey("btn_ManagerQuestion_Edit", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddNew.Text = GetLanageValueByKey("theme_BBS_AddTheme", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private void BandTitle()
    {
        string title = string.Empty;
        PowerCreator.LMS.BBS.Entity.VOTheme voTheme = new VOTheme();
        voTheme = opBBS.LoadTheme(int.Parse(ViewState["ThemeID"].ToString()));
        this.Title = voTheme.ThemeTitle;
        lbl_titleinfo.Text = voTheme.ThemeTitle;
        if (voTheme.ThemeAnswerState == 0)
        {
            lab_Title.Text = voTheme.ThemeTitle + "<a style=\"color:Red\">" + config.GetLanagerValueByKey("theme_Theme_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page) + "</a>";
        }
        else
        {
            lab_Title.Text = voTheme.ThemeTitle;
        }
        lab_Author.Text = voTheme.ThemeUserName.ToString();
    
        if (voTheme.AccessoryType == 0)
        {
            lab_AccessoryName.Text = string.Format(AccessoryVedio, "/fileManager/UploadFile/BBS/" + voTheme.AccessoryRUL);
        }
        else
        {
            lab_AccessoryName.Text =string.Format(Accessory, "/fileManager/UploadFile/BBS/" + voTheme.AccessoryRUL, voTheme.AccessoryName);
        }
        
        lab_Content.Text = voTheme.ThemeContent;
        lab_DateCreated.Text = voTheme.ThemeCreatetime.ToShortDateString();
        voTheme.ThemeVisitNum += 1;
        lab_ReadCount.Text = Convert.ToString(voTheme.ThemeVisitNum + 1);
        opBBS.UpdateTheme(voTheme);
        //BinderNewTheme(voTheme.ThemeDomainID);
        ViewState["DomainID"] = voTheme.ThemeDomainID.ToString();

        btn_AddNew.Visible = false;
        btn_Edit.Visible = false;

        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] != null)
        {
            PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (voTheme.ThemeUserID == login.UserID)
            {
                btn_Edit.Visible = true;
                btn_AddNew.Visible = true;
            }
        }
    }

    ////绑定最新回复
    //private void BinderNewTheme(int domainid)
    //{
    //    string search =string.Format("ThemeDomainID = {0} and ThemeIsVisible = 1",domainid.ToString());
    //    string orderList= "ThemeLastRevertTime desc ";
    //    DataTable dt = opBBS.QueryTheme(search, orderList, PowerCreator.LMS.Web.Config.WebConfig.PageSize, 1);
    //    rpt_Theme.DataSource = dt;
    //    rpt_Theme.DataBind();
    //}


    private string GetSearch()
    {
        string search = " ResversionThemeID=" + ViewState["ThemeID"].ToString();
        return search.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = opBBS.CountTheme(search);
        AspNetPager1.PageSize = PowerCreator.LMS.Web.Config.WebConfig.PageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        dt = opBBS.QueryReversion(search, PowerCreator.LMS.Web.Config.WebConfig.PageSize, pageIndex);
        dt.Columns.Add("num");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["AccessoryType"].ToString() == "0")
            {
                dt.Rows[i]["AccessoryName"] = string.Format(AccessoryVedio, "/fileManager/UploadFile/BBS/" + dt.Rows[i]["AccessoryRUL"].ToString());
            }
            else
            {
                dt.Rows[i]["AccessoryName"] = string.Format(Accessory, "/fileManager/UploadFile/BBS/" + dt.Rows[i]["AccessoryRUL"].ToString(), dt.Rows[i]["AccessoryName"].ToString());
            }
            dt.Rows[i]["num"] = (i+1).ToString();
        }
        
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void btn_seach_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    /// <summary>
    ///  获取上传资源时文件夹路径
    /// </summary>
    /// <param name="courseID">课程ID</param>
    /// <param name="srt">资源类型</param>
    /// <returns></returns>
    public string GetFileName()
    {
        Random objRand = new Random();
        System.DateTime date = DateTime.Now;
        //生成随机文件名
        string saveName = date.ToString("yyyyMMddHHmmsss") + Convert.ToString(objRand.Next(99) * 97 + 100);

        return saveName;
    }
    protected void btn_Answer_Click(object sender, EventArgs e)
    {
        if (lab_AnswerState.Text == "0")
        {
            Script.Alert(this, "msg", config.GetLanagerValueByKey("theme_Theme_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.Alert(this, "msg", config.GetLanagerValueByKey("theme_Theme_Msg3", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }

        if (string.IsNullOrEmpty(txt_Answer.Text.Trim()))
        {
            Script.Alert(this, "msg", config.GetLanagerValueByKey("theme_Theme_Msg4", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        string fileName = string.Empty;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Path = Server.MapPath("../../fileManager/UploadFile/BBS/");

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            //组合路径，file.GetName()取得文件名
            fileName = GetFileName() + file.GetExtension();

            Path = Path + fileName;

            //保存
            file.SaveAs(Path, true);
            int filesize = file.ContentLength;

            //string url = "AddDownload.aspx?AddDownload=upload&DownloadID=" + ViewState["DownloadID"].ToString() + "&fullpath=fileManager/downloadFile/" + fileName + "&filesize=" + filesize;
            //Script.RedirectURL(url);
        }

        PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];

        VOTheme objVOTheme = new VOTheme();
        objVOTheme = opBBS.LoadTheme(int.Parse(ViewState["ThemeID"].ToString()));
        objVOTheme.ThemeReversionNum += 1;
        objVOTheme.ThemeLastRevertTime = DateTime.Now;
        objVOTheme.ThemeLastRevertUserID = login.UserID;
        objVOTheme.ThemeLastRevertUserName = login.RealName;

        if (!opBBS.UpdateTheme(objVOTheme))
        {
            Script.Alert(this, "msg", config.GetLanagerValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }

        VOReversion objVOReversion = new VOReversion();

        objVOReversion.ResversionThemeID = int.Parse(ViewState["ThemeID"].ToString());
        objVOReversion.ResversionUserID = login.UserID;
        objVOReversion.ResversionUserName = login.RealName;
        objVOReversion.ResversionContent = txt_Answer.Text;
        objVOReversion.AccessoryName = txt_AccessoryName.Text;
        objVOReversion.AccessoryRUL = fileName;
        objVOReversion.AccessoryType = int.Parse(dpdlst_AccessoryType.SelectedValue);

        objVOReversion.ResversionCreateTime = DateTime.Now;


        if (opBBS.CreateReversion(objVOReversion))
        {
            string search = GetSearch();
            BinderGV(search, AspNetPager1.CurrentPageIndex);
            txt_Answer.Text = "";
            Script.Alert(this, "msg", config.GetLanagerValueByKey("theme_Theme_Msg5", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        else
        {
            Script.Alert(this, "msg", config.GetLanagerValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
    protected void btn_RturnIndex_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("BBS.aspx?Type=" + ViewState["Type"].ToString() + "&DomainID=" + ViewState["DomainID"].ToString());
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("EditTheme.aspx?Type=" + ViewState["Type"].ToString() + "&ThemeID=" + ViewState["ThemeID"].ToString());
    }
    protected void btn_AddNew_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("AddTheme.aspx?Type=" + ViewState["Type"].ToString() + "&DomainID=" + ViewState["DomainID"].ToString());
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("BBS.aspx?Type=" + ViewState["Type"].ToString() + "&DomainID=" + ViewState["DomainID"].ToString());
    }
}
